home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / MPW / gzip 1.2.2 / zip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-24  |  3.1 KB  |  122 lines  |  [TEXT/MPS ]

  1. /* zip.c -- compress files to the gzip or pkzip format
  2.  * Copyright (C) 1992-1993 Jean-loup Gailly
  3.  * This is free software; you can redistribute it and/or modify it under the
  4.  * terms of the GNU General Public License, see the file COPYING.
  5.  */
  6.  
  7. #ifndef lint
  8. static char rcsid[] = "$Id: zip.c,v 0.17 1993/06/10 13:29:25 jloup Exp $";
  9. #endif
  10.  
  11. #include <ctype.h>
  12. #ifdef macintosh
  13. #include <types.h>
  14. #else
  15. #include <sys/types.h>
  16. #endif
  17.  
  18. #include "tailor.h"
  19. #include "gzip.h"
  20. #include "crypt.h"
  21.  
  22. #ifdef HAVE_UNISTD_H
  23. #  include <unistd.h>
  24. #endif
  25. #ifndef NO_FCNTL_H
  26. #  include <fcntl.h>
  27. #endif
  28.  
  29. local ulg crc;       /* crc on uncompressed file data */
  30. long header_bytes;   /* number of bytes in gzip header */
  31.  
  32. /* ===========================================================================
  33.  * Deflate in to out.
  34.  * IN assertions: the input and output buffers are cleared.
  35.  *   The variables time_stamp and save_orig_name are initialized.
  36.  */
  37. int zip(in, out)
  38.     int in, out;            /* input and output file descriptors */
  39. {
  40.     uch  flags = 0;         /* general purpose bit flags */
  41.     ush  attr = 0;          /* ascii/binary flag */
  42.     ush  deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
  43.  
  44.     ifd = in;
  45.     ofd = out;
  46.     outcnt = 0;
  47.  
  48.     /* Write the header to the gzip file. See algorithm.doc for the format */
  49.  
  50.     method = DEFLATED;
  51.     put_byte(GZIP_MAGIC[0]); /* magic header */
  52.     put_byte(GZIP_MAGIC[1]);
  53.     put_byte(DEFLATED);      /* compression method */
  54.  
  55.     if (save_orig_name) {
  56.     flags |= ORIG_NAME;
  57.     }
  58.     put_byte(flags);         /* general flags */
  59.     put_long(time_stamp);
  60.  
  61.     /* Write deflated file to zip file */
  62.     crc = updcrc(0, 0);
  63.  
  64.     bi_init(out);
  65.     ct_init(&attr, &method);
  66.     lm_init(level, &deflate_flags);
  67.  
  68.     put_byte((uch)deflate_flags); /* extra flags */
  69.     put_byte(OS_CODE);            /* OS identifier */
  70.  
  71.     if (save_orig_name) {
  72.     char *p = basename(ifname); /* Don't save the directory part. */
  73.     do {
  74.         put_char(*p);
  75.     } while (*p++);
  76.     }
  77.     header_bytes = (long)outcnt;
  78.  
  79.     (void)deflate();
  80.  
  81. #if !defined(NO_SIZE_CHECK) && !defined(RECORD_IO)
  82.   /* Check input size (but not in VMS -- variable record lengths mess it up)
  83.    * and not on MSDOS -- diet in TSR mode reports an incorrect file size)
  84.    */
  85.     if (ifile_size != -1L && isize != (ulg)ifile_size) {
  86.     Trace((stderr, " actual=%ld, read=%ld ", ifile_size, isize));
  87.     fprintf(stderr, "%s: %s: file size changed while zipping\n",
  88.         progname, ifname);
  89.     }
  90. #endif
  91.  
  92.     /* Write the crc and uncompressed size */
  93.     put_long(crc);
  94.     put_long(isize);
  95.     header_bytes += 2*sizeof(long);
  96.  
  97.     flush_outbuf();
  98.     return OK;
  99. }
  100.  
  101.  
  102. /* ===========================================================================
  103.  * Read a new buffer from the current input file, perform end-of-line
  104.  * translation, and update the crc and input file size.
  105.  * IN assertion: size >= 2 (for end-of-line translation)
  106.  */
  107. int file_read(buf, size)
  108.     char *buf;
  109.     unsigned size;
  110. {
  111.     unsigned len;
  112.  
  113.     Assert(insize == 0, "inbuf not empty");
  114.  
  115.     len = read(ifd, buf, size);
  116.     if (len == (unsigned)(-1) || len == 0) return (int)len;
  117.  
  118.     crc = updcrc((uch*)buf, len);
  119.     isize += (ulg)len;
  120.     return (int)len;
  121. }
  122.